//functions.cpp
/********************************************************************************
*																				*
*	Author:			Jared Szechy												*
*	Assignment:		Assignment 1												*
*	Class:			CPS-151														*
*	Instructor:		V. Starkley													*
*	Due Date:		August 30													*
*																				*
*	This file implements the functions used throughout the program.				*
*																				*
********************************************************************************/

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "functions.h"
using namespace std;

void readItem(ifstream& fin, int itemNum, Dispenser disp[])
{
	string tempName;
	int tempInt;
	double tempDouble;

	while(isspace(fin.peek()))
		fin.ignore();
	getline(fin,tempName);
	disp[itemNum].setProductName(tempName);
	fin >> tempDouble;
	disp[itemNum].setCost(tempDouble);
	fin >> tempInt;
	disp[itemNum].setQuantityOnHand(tempInt);
}

void printInventory(ostream& fout, int numItems, Dispenser disp[])
{
	fout << endl << "Current Inventory" << endl << endl;
	fout << setw(5) << "Item" << setw(20) << "Name" << setw(10) << "Cost" << setw(10) << "Quantity" << endl;

	for (int i = 0; i < numItems; i++)
	{
		fout << setw(5) << i << setw(20) << disp[i].getProductName() << setw(10) << disp[i].getCost() << setw(10)
			 << disp[i].getQuantityOnHand() << endl;
	}
}

void tender(ostream&fout, int itemNum, Dispenser disp[], CashRegister& cash)
{
	double tempCash;

	fout << endl << "You have selected: " << disp[itemNum].getProductName() << endl;
	fout << "The price is: $" << disp[itemNum].getCost() << endl << endl;
	fout << "Please deposit money." << endl;
	while(cash.getCashOnHand() < disp[itemNum].getCost())
	{
		fout << endl << "Cash deposited: $" << cash.getCashOnHand() << endl;
		fout << "Enter coin values: ";
		cin >> tempCash;
		cash.setCashOnHand(cash.getCashOnHand() + tempCash);
	}
	cash.setCashOnHand(cash.getCashOnHand() - disp[itemNum].getCost());
	disp[itemNum].makeSale();
	cout << disp[itemNum].getProductName() << " have been dispensed." << endl;
}

void writeInventory(ostream& fout, int numItems, Dispenser disp[])
{
	for (int i = 0; i < numItems; i++)
	{
		fout << disp[i].getProductName() << endl;
		fout << disp[i].getCost() << endl;
		fout << disp[i].getQuantityOnHand() << endl;
	}
}